//
//  JHHotMusicViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHHotMusicViewController.h"
#import "JHHotMusicTableViewCell.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "NetwordHandler.h"
#import "JHPlayViewController.h"
#import "JHgequ.h"

@interface JHHotMusicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *HotMusicTableView;
@property(nonatomic,retain)UISegmentedControl *HotUMusicSegmented;
@property(nonatomic,retain)NSString *s;
@property(nonatomic,retain)NSMutableArray *HotMusicArr;
@end

@implementation JHHotMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.HotMusicArr = [NSMutableArray array];
    self.nextCursor = 1;
    self.s = @"daily";
    [self createHotMusicSegmented];
    [self createHotMusicTableView];
//        [self addHeader];
    [self addHeader];
    [self addFooter];
    
}
- (void)createHotMusicSegmented
{
    /**
     :returns: 创建分段控制器
     */
    self.HotUMusicSegmented = [[UISegmentedControl alloc] initWithItems:@[@"最火",@"本周热门",@"最多赞"]];
    _HotUMusicSegmented.frame = CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height / 20);
//    _HotUMusicSegmented.selectedSegmentIndex = 0;
    [_HotUMusicSegmented addTarget:self action:@selector(segmenAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_HotUMusicSegmented];
    [_HotUMusicSegmented release];

}
- (void)segmenAction:(UISegmentedControl *)seg
{
    
    //  根据分段标签坐标判断方法
    if (seg.selectedSegmentIndex == 0) {
        NSLog(@"最火");
        _s = @"daily";

        [self addHeader];
        [self addFooter];
        
    }else
    if (seg.selectedSegmentIndex == 1) {
        NSLog(@"本周热门");
        _s = @"hot";
        [self addHeader];
        [self addFooter];
        
    }else
        if (seg.selectedSegmentIndex == 2) {
            NSLog(@"最多赞");
            _s = @"favorite";
            [self addHeader];
            [self addFooter];
           
        }
//    [self getData1];
}

- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block JHHotMusicViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.HotMusicTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:1];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.HotMusicTableView headerBeginRefreshing];
    
}
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block JHHotMusicViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.HotMusicTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonlist:(vc.nextCursor++) + 1];//请求更多数据
        
    }];
}
- (void)GetPersonList:(NSInteger)nextCursor
{
    /**
     *  刷新
     */
    NSString *a = _s;
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_track_list?category_name=all&condition=%@&device=android&page=%ld&per_page=15&tag_name=",a,nextCursor];
    [NetwordHandler requestWithURL:str completion:^(id result) {
//        NSLog(@"aaaaaaaaaaaaaaaaaaa%@",result);
        if (self.isUpLoading == NO) {
            //说明是下拉刷新，就要清空数组中的数据，保持数组中存放的是最新的数据
            [self.HotMusicArr removeAllObjects];
        }

        NSDictionary *dic = result;
        NSMutableArray *a = [dic objectForKey:@"list"];
        for (NSDictionary *d in a) {
            JHHotMucis *music = [[JHHotMucis alloc] initWithDic:d];
            [_HotMusicArr addObject:music];
        }
        NSLog(@"%@",_HotMusicArr);
//        [self createHotMusicTableView];
        [self.HotMusicTableView headerEndRefreshing];
        [self.HotMusicTableView footerEndRefreshing];
        [self.HotMusicTableView reloadData];
        
        
    }];
}
- (void)GetPersonlist:(NSInteger)nextCursor
{
    /**
     *  加载
     */
    NSString *a = _s;
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_track_list?category_name=all&condition=%@&device=android&page=%ld&per_page=15&tag_name=",a,nextCursor];
    [NetwordHandler requestWithURL:str completion:^(id result) {
        NSLog(@"%@",result);
        
        NSDictionary *dic = result;
        NSMutableArray *a = [dic objectForKey:@"list"];
        for (NSDictionary *d in a) {
            JHHotMucis *music = [[JHHotMucis alloc] initWithDic:d];
            [_HotMusicArr addObject:music];
        }
        NSLog(@"%@",_HotMusicArr);
        //        [self createHotMusicTableView];
 
        [self.HotMusicTableView footerEndRefreshing];
        [self.HotMusicTableView reloadData];
        
        
    }];
}

- (void)createHotMusicTableView
{
    //  创建tableView
    self.HotMusicTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,_HotUMusicSegmented.frame.size.height + 20, self.view.frame.size.width, self.view.frame.size.height - _HotUMusicSegmented.frame.size.height - 74 - 40) style:UITableViewStyleGrouped];
    _HotMusicTableView.delegate = self;
    _HotMusicTableView.dataSource= self;
    
    _HotMusicTableView.separatorColor = [UIColor clearColor];
    [_HotMusicTableView registerClass:[JHHotMusicTableViewCell class] forCellReuseIdentifier:@"HotMusic"];
    [self.view addSubview:_HotMusicTableView];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  重用池
    JHHotMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotMusic"];
  
    cell.hotMusic = _HotMusicArr[indexPath.section];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //  行数
    return 1 ;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    /**
     *  分区数
     */
    return _HotMusicArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  行高
//    JHHotMucis *s = [[JHHotMucis alloc ]init];
    CGFloat f = [JHHotMusicTableViewCell heightWithText:[_HotMusicArr[indexPath.section] title]];
    return f + 86;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  点击方法
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    JHPlayViewController *p = [JHPlayViewController sharePlayerController];
    JHgequ *ss = [[JHgequ alloc] init];
    ss.playPath32 = [_HotMusicArr[indexPath.section] playPath32];
    ss.title = [_HotMusicArr[indexPath.section]title] ;
//    p.str = ss;
    [p setStr:ss];
    [p beginAction:self];
    NSLog(@"%@",_HotMusicArr[indexPath.section]);
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"zhuan" object:@"jiaqing" userInfo:@{@"lol":@"666"}];
    [self presentViewController:p animated:YES completion:^{
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
